namespace Digitalmes.Application.Sys.Dicts.Commands;

/// <summary>
/// 更新字典命令。
/// </summary>
public record UpdateDictCommand(long Id, SysDictDto Dict) : IRequest<IResult<bool>>;

internal sealed class UpdateDictCommandHandler(IRepository<SysDict> repo) : IRequestHandler<UpdateDictCommand, IResult<bool>>
{
    public async Task<IResult<bool>> Handle(UpdateDictCommand request, CancellationToken cancellationToken)
    {
        var dict = await repo.GetByIdAsync(request.Id, cancellationToken);
        if (dict == null)
        {
            return Result.Failure("未找到要更新的字典");
        }

        dict.Name = request.Dict.Name;
        dict.Status = request.Dict.Status;

        var ok = await repo.UpdateAsync(dict, m => new {
            m.Name,
            m.Status,
            m.UpdatedBy,
            m.UpdatedTime,
        }, cancellationToken);
        return Result.From(ok);
    }
}
